try:
    from PIL import Image 
except Exception, e:
    import Image
try:
    from pyqrcode import QRCode as QRCode2
except Exception, e:
    print e

import subprocess

import sys, os, platform, io
OS = platform.system()


QR_DIR = '.'
try:
    # b = u'\u2588'.encode(sys.stdin.encoding)
    b = u'\u2588'
    sys.stdout.write(b + '\r')
    sys.stdout.flush()
except:
    BLOCK = 'MM'
else:
    BLOCK = b

class QRCode():
    def __init__(self,fileName, size, padding = 0, background = 'BLACK', qrText = None):
        self.size = size
        self.padding = padding
        self.img = Image.open(fileName)
        self.times = self.img.size[0]/(size + padding * 2)
        self.rgb = self.img.convert('RGB')
        self.white = BLOCK if background == 'BLACK' else '  '
        self.black = '  ' if background == 'BLACK' else BLOCK 
        self.fileName = fileName
        self.qrText = qrText

    def print_qr(self):
        fileDir = self.fileName
        if OS == 'Darwin':
            subprocess.call(['open', fileDir])
        elif OS == 'Linux':
            subprocess.call(['xdg-open', fileDir])
        else:
            os.startfile(fileDir)

    def print_cmd_qr(self, enableCmdQR=True):
        white = self.white
        black = self.black
        qrText = self.qrText 
        blockCount = int(enableCmdQR)
        if abs(blockCount) == 0:
            blockCount = 1
        white *= abs(blockCount)
        if blockCount < 0:
            white, black = black, white
        sys.stdout.write(' '*50 + '\r')
        sys.stdout.flush()
        qr = qrText.replace('0', white).replace('1', black)
        sys.stdout.write(qr)
        sys.stdout.flush()

    # def print_qr(self):
    #     sys.stdout.write(' '*50 + '\r')
    #     sys.stdout.flush()
    #     print self.white
    #     print(self.white * (self.size + 2))
    #     startPoint = self.padding + 0.5
    #     for y in range(self.size):
    #         sys.stdout.write(self.white)
    #         for x in range(self.size):
    #             r,g,b = self.rgb.getpixel(((x + startPoint) * self.times, (y + startPoint) * self.times))
    #             sys.stdout.write(self.white if r > 127 else self.black)
    #         print(self.white)
    #     print(self.white * (self.size + 2))

if __name__ == '__main__':
    # 37 is for picture size without padding, 3 is padding
    q = QRCode(os.path.join(os.path.pardir, 'log', 'QR.jpg'), 37, 3, 'BLACK')
    q.print_qr()